﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HookLock;
using System.Xml;
using System.Globalization;
using System.Diagnostics;
using System.Text.RegularExpressions;


namespace 挂机锁
{
    public partial class LOCK : Form
    {
        int timeout;
        string pwd;
        int time;

        public int Time
        {
            get
            {
                time -= 1; 
                return time; 
            }
            set { time = value; }
        }

        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }

        public int Timeout
        {
            get { return timeout; }
            set { timeout = value*60*1000; }
        }

        Hook look = new Hook();
        public LOCK()
        {
            InitializeComponent();
        }

        private void LOCK_Load(object sender, EventArgs e)
        {
            XmlDocument xmlfile = new XmlDocument();
            xmlfile.Load(@"config.xml");
            XmlNodeList elemList2 = xmlfile.GetElementsByTagName("timeout");
            for (int i = 0; i < elemList2.Count; i++)
            {
               Timeout=Convert.ToInt32(elemList2[i].InnerXml);
               Time = Convert.ToInt32(elemList2[i].InnerXml)*60;
            }
            timer1.Interval = Timeout;
            look.InstallHook();
            this.BringToFront();
        }

        private void LOCK_FormClosing(object sender, FormClosingEventArgs e)
        {
            look.UnInstallHook();
           //Setting.pnw.timer1.Stop();
           Setting.pnw.timer1.Start();
           Setting.pnw.Time = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
            look.UnInstallHook();
        }

   

        private void button1_Click(object sender, EventArgs e)
        {

            XmlDocument xmlfile = new XmlDocument();
            xmlfile.Load(@"config.xml");
            XmlNodeList elemList2 = xmlfile.GetElementsByTagName("pwd");
            for (int i = 0; i < elemList2.Count; i++)
            {
                Pwd = elemList2[i].InnerXml;
            }
            if (Pwd == textBox1.Text)
            {
                this.Close();
                look.UnInstallHook();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            lbl倒计时.Text = Time.ToString();
            Process[] a = Process.GetProcesses();
            foreach (Process b in a)
            {
                if (b.ProcessName == "taskmgr")
                    b.Kill();
            }
            this.BringToFront();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Regex a = new Regex("[^0-9a-zA-Z]");
            if (a.IsMatch(textBox1.Text))
            {
                MessageBox.Show("只能是数字或字母");
                textBox1.Text = "";
            }
        }
    }
}
